﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using SharpBolt.Graphics;
using System.Windows.Forms;
using SharpBolt;
using SharpBolt.Graphics.GraphicInstances;
using System.IO;
using SharpBolt.Graphics.GraphicSources;

namespace SharpBolt.Editing
{
    [Editor(typeof(Editing.UISpriteSourceEditor), typeof(System.Drawing.Design.UITypeEditor))]
    class UISpriteSourceEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
        public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            if (svc != null)
            {
                SpriteSource SpriteSource = value as SpriteSource;
                string resourcepath = Engine.ResourcesPrefix + SpriteSource.Name;
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.InitialDirectory = Path.GetFullPath(Engine.ResourcesPrefix);
                ofd.Filter = "Sprite|*.spr";//|Image|*.png;*.jpg;*.bmp;*.gif";
                if (ofd.ShowDialog() != DialogResult.OK) return value;
                string shortName = ofd.FileName;

                if (!shortName.Contains(Engine.ResourcesPrefix)) return value;
                shortName = shortName.Remove(0, shortName.LastIndexOf(Engine.ResourcesPrefix) + Engine.ResourcesPrefix.Length + 1);
                SpriteSource NewSpriteSource = SpriteSource.LoadSprite(shortName);
                return NewSpriteSource;
            }

            return value;
        }
    }
}
